package cn.gbase.jiangsu.data.transfer.util;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.nutz.mvc.Mvcs;

import cn.gbase.jiangsu.data.transfer.bean.ShopUser;
import cn.gbase.jiangsu.data.transfer.bean.SystemUser;

public class CommonUtil {

	/**
	 * 取得当前登录用户名.
	 * 
	 * @return
	 */
	public static String getLoginUserName() {
		return ((SystemUser) Mvcs.getReq().getSession(false).getAttribute("user")).getUsername();
	}

	/**
	 * 取得商城当前登录用户ID.
	 * 
	 * @return
	 */
	public static int getShopLoginUserId() {
		return ((ShopUser) Mvcs.getReq().getSession(false).getAttribute("shopUser")).getId();
	}

	/**
	 * 商城用户是否登录.
	 * 
	 * @return
	 */
	public static boolean isShopUserLogin() {
		HttpSession session = Mvcs.getReq().getSession(false);
		if (session == null) {
			return false;
		}
		return session.getAttribute("shopUser") != null;
	}

	/**
	 * 格式化楼号（两位）
	 * 
	 * @param buildingNo
	 *            楼号
	 * @return
	 */
	public static String formatBuildingNo(String buildingNo) {
		if (buildingNo == null || buildingNo.length() == 0) {
			return "00";
		}
		if (buildingNo.length() >= 2) {
			return buildingNo.substring(0, 2);
		}
		return "0" + buildingNo;
	}

	/**
	 * 格式化单元号（两位）
	 * 
	 * @param unitNo
	 *            单元号
	 * @return
	 */
	public static String formatUnitNo(String unitNo) {
		if (unitNo == null || unitNo.length() == 0) {
			return "00";
		}
		if (unitNo.length() >= 2) {
			return unitNo.substring(0, 2);
		}
		return "0" + unitNo;
	}

	/**
	 * 格式化楼层号（2位）
	 * 
	 * @param unitNo
	 *            单元号
	 * @return
	 */
	public static String formatFloorNo(String floorNo) {
		if (floorNo == null || floorNo.length() == 0) {
			return "00";
		}
		if (floorNo.length() >= 2) {
			return floorNo.substring(0, 2);
		}
		return "0" + floorNo;
	}

	/**
	 * 格式化房间号（2位）
	 * 
	 * @param unitNo
	 *            单元号
	 * @return
	 */
	public static String formatRoomNo(String roomNo) {
		if (roomNo == null || roomNo.length() == 0) {
			return "00";
		}
		if (roomNo.length() >= 2) {
			return roomNo.substring(0, 2);
		}
		return "0" + roomNo;
	}

	/**
	 * 根据房间完整编码生成验证码（对于单个房间，每次生成结果相同）.
	 * 
	 * @param code
	 *            房间完整编码
	 * @return 验证码
	 */
	public static String createBindCode(String code) {
		DefaultPasswordEncoder encoder = new DefaultPasswordEncoder("MD5");
		String bindCode = encoder.encode("Hello" + code); // 结果是32位的字符串
		return encoder.encode(bindCode).substring(5, 15).toUpperCase(); // 取10位
	}

	/**
	 * 用户密码加密.
	 * 
	 * @param pw
	 *            原密码
	 * @return 加密后密码
	 */
	public static String encodePw(String pw) {
		//		return new DefaultPasswordEncoder("MD5").encode("gaga" + pw).substring(
		//				8, 28);
		return new DefaultPasswordEncoder("MD5").encode(pw);
	}

	/**
	 * 用户密码加密.
	 * 
	 * @param pw
	 *            原密码
	 * @return 加密后密码
	 */
	public static String encodeUserPw(String pw) {
		return new DefaultPasswordEncoder("MD5").encode("gaga" + pw).substring(8, 28);
	}

	/**
	 * 随机数字的字符串.
	 * 
	 * @param length
	 *            长度
	 * @return 随机字符串
	 */
	public static String getRandomNumString(int length) {
		String base = "0123456789abcdefghijklmnopqrstuvwxyz";
		Random random = new Random(new Date().getTime());
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 生成订单号.
	 * 
	 * @return 订单号
	 */
	public static String createOrderCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		return sdf.format(new Date()) + getRandomNumString(8);
	}

	/**
	 * 取得客户端IP地址.
	 * 
	 * @return
	 */
	public static String getRemoteIp() {
		return Mvcs.getReq().getRemoteAddr();
	}

	/**
	 * 格式化房间号（4位）
	 * 
	 * @param roomNo
	 *            房间号
	 * @return
	 */
	public static String formatRoomNo0000(String roomNo) {
		if (roomNo == null || roomNo.length() == 0) {
			return "0000";
		}
		if (roomNo.length() >= 4) {
			return roomNo.substring(0, 4);
		}
		if (roomNo.length() == 3) {
			return "0" + roomNo;
		}
		if (roomNo.length() == 2) {
			return "00" + roomNo;
		}
		return "000" + roomNo;
	}

	/**
	 * 取得今天零点的时刻.
	 * 
	 * @return
	 */
	public static Date getToday000000() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 取得今天之后，x天零点的时刻.
	 * 
	 * @return
	 */
	public static Date getTodayAddDay000000(int addDays) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DAY_OF_YEAR, addDays); // 加x天
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 数字前面补0.
	 * 
	 * @param number
	 *            数字
	 * @param fixLength
	 *            结果的位数
	 * @return
	 */
	public static String fixLengthNumber(int number, int fixLength) {
		NumberFormat f = NumberFormat.getIntegerInstance();
		f.setGroupingUsed(false);
		f.setMaximumIntegerDigits(fixLength);
		f.setMinimumIntegerDigits(fixLength);
		return f.format(number);
	}

}
